﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YYS
{
    using System.Runtime.Serialization;

    /// <summary>
    /// 返回结果对象接口
    /// </summary>
    public interface IResultObj
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        int ErrorCode { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        string Error { get; set; }
    }

    /// <summary>
    /// 返回结果对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [DataContract(Name = "Result_{0}{#}")]
    public class ResultObj<T> : IResultObj
    {
        int _ErrorCode = 0;
        /// <summary>
        /// 错误代码
        /// </summary>
        [DataMember]
        public int ErrorCode
        {
            get { return _ErrorCode; }
            set { _ErrorCode = value; }
        }

        private string _Error = null;
        /// <summary>
        /// 错误信息
        /// </summary>
        [DataMember]
        public string Error
        {
            get { return _Error; }
            set { _Error = value; }
        }

        private string _Mark = string.Empty;
        /// <summary>
        /// 备注
        /// </summary>
        [DataMember]
        public string Mark
        {
            get { return _Mark; }
            set { _Mark = value; }
        }

        private T _Obj;
        /// <summary>
        /// 单个对象
        /// </summary>
        [DataMember]
        public T Obj
        {
            get { return _Obj; }
            set { _Obj = value; }
        }

        private List<T> _ObjList = new List<T>();
        /// <summary>
        /// 列表对象
        /// </summary>
        [DataMember]
        public List<T> ObjList
        {
            get { return _ObjList; }
            set { _ObjList = value; }
        }

        #region 分页属性

        int _RowCount = 0;
        /// <summary>
        /// 总条数
        /// </summary>
        [DataMember]
        public int RowCount
        {
            get { return _RowCount; }
            set { _RowCount = value; }
        }
        int _PageCount = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        [DataMember]
        public int PageCount
        {
            get { return _PageCount; }
            set { _PageCount = value; }
        }
        /// <summary>
        /// 每页记录数
        /// </summary>
        [DataMember]
        public int PageSize { get; set; }

        int _PageIndex = 1;
        /// <summary>
        /// 当前页
        /// </summary>
        [DataMember]
        public int PageIndex
        {
            get { return _PageIndex; }
            set { _PageIndex = value; }
        }

        #endregion

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="error">错误信息</param>
        /// <param name="errorCode">自定义错误代码</param>
        /// <param name="IsSystemError">是否视为系统错误</param>
        /// <returns></returns>
        public ResultObj<T> SetError(object error, int errorCode = 0, bool IsSystemError = false)
        {
            this.Error = error == null ? null : ((IsSystemError ? "[SystemError]" : "") + error.ToString());
            this.ErrorCode = errorCode;
            return this;
        }
        /// <summary>
        /// 是否包含错误
        /// </summary>
        public bool HasError
        {
            get
            {
                return this.Error != null;
            }
        }

        /// <summary>
        /// 判断是否系统错误
        /// </summary>
        public bool HasSystemError
        {
            get
            {
                return this.HasError && this.Error.IndexOf("SystemError", StringComparison.OrdinalIgnoreCase) > -1;
            }
        }
    }

    /// <summary>
    /// 返回结果对象（扩展）,继承ResultObj
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [DataContract(Name = "Result_{0}{#}")]
    public class ExtendResultObj<T> : ResultObj<T>
    {
        object _ExtendField1 = null;
        /// <summary>
        /// 扩展字段1
        /// </summary>
        [DataMember]
        public object ExtendField1
        {
            get { return _ExtendField1; }
            set { _ExtendField1 = value; }
        }

        object _ExtendField2 = null;
        /// <summary>
        /// 扩展字段2
        /// </summary>
        [DataMember]
        public object ExtendField2
        {
            get { return _ExtendField2; }
            set { _ExtendField2 = value; }
        }

        object _ExtendField3 = null;
        /// <summary>
        /// 扩展字段3
        /// </summary>
        [DataMember]
        public object ExtendField3
        {
            get { return _ExtendField3; }
            set { _ExtendField3 = value; }
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="error">错误信息</param>
        /// <param name="errorCode">自定义错误代码</param>
        /// <param name="IsSystemError">是否视为系统错误</param>
        /// <returns></returns>
        public new ExtendResultObj<T> SetError(object error, int errorCode = 0, bool IsSystemError = false)
        {
            base.SetError(error, errorCode, IsSystemError);
            return this;
        }
    }
}
